package com.example.androidworklhz.act

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.viewpager2.widget.ViewPager2
import com.example.androidworklhz.R
import com.example.androidworklhz.adapter.WelcomeAdapter

class WelcomeActivity : AppCompatActivity() {
    // 声明组件
    private lateinit var vpWelcome: ViewPager2

    // 引导页用到的图片资源
    private var imgRes = mutableListOf<Int>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.enableEdgeToEdge()
        setContentView(R.layout.activity_welcome)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v: View, insets: WindowInsetsCompat ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        // 找到分页组件
        vpWelcome = findViewById(R.id.vpWelcome)
        // 准备图片资源
        imgRes.add(R.mipmap.w1)
        imgRes.add(R.mipmap.w2)
        imgRes.add(R.mipmap.w3)
        imgRes.add(R.mipmap.w4)
        // 创建Adapter实例
        val adapter = WelcomeAdapter(imgRes)
        vpWelcome.adapter = adapter
        vpWelcome.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                // 检查是否在最后一页，如果是则延迟进入 LoginActivity
                if (position == imgRes.size - 1) {
                    Handler().postDelayed({
                        startActivity(Intent(this@WelcomeActivity, LoginActivity_XXT::class.java))
                        finish()
                    }, 1000)
                }
            }
        })
    }
}